package com.sky.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("myAspect")
@Aspect //标注当前Aspect是一个切面类
public class MyAspect {

    //TODO 定义切点表达式
    @Pointcut("execution(* com.sky.anno.*.*(..))")
    public void pointcut() {
    }

    //配置前置通知
    @Before(value = "pointcut()")
    public void before() {
        System.out.println("前置增强...");
    }

    @AfterReturning(value = "MyAspect.pointcut()")
    public void afterReturning() {
        System.out.println("后置增强...");
    }

    @Around(value = "execution(* com.sky.anno.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强....");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强...");
        return proceed;
    }

    @AfterThrowing(value = "execution(* com.sky.anno.*.*(..))")
    public void afterThrowing() {
        System.out.println("异常抛出增强...");
    }

    @After(value = "execution(* com.sky.anno.*.*(..))")
    public void after() {
        System.out.println("最终增强...");
    }
}
